#include "MainMenuState.h"

#include "Game.h"
#include "GamePlayState.h"
#include "CreditsState.h"
#include "OptionsState.h"
#include "TutorialState.h"
#include "tinyxml.h"

MainMenuState::MainMenuState(void)
{
	// Set data members to 'clean' default values
	D3D = nullptr;
	DI  = nullptr;
	TM  = nullptr;
	XA  = nullptr;

	GroupFont = nullptr;

	// -1 == INVALID ID
	FontImageID = -1;

	CursorX = -1;
	CursorY = -1;
}

MainMenuState::~MainMenuState(void)
{
}

void MainMenuState::Enter(void)
{
	// Acess all the SGD Wrappers
	D3D	=	CSGD_Direct3D::GetInstance();
	DI	=	CSGD_DirectInput::GetInstance();
	TM	=	CSGD_TextureManager::GetInstance();
	XA	=	CSGD_XAudio2::GetInstance();

	// Getting group font
	GroupFont =	CBitmapFont::GetInstance();

	// Load resources
	FontImageID = TM->LoadTexture(_T("Graphics/Robovox.bmp"), D3DCOLOR_XRGB(0,0,0));

	// Set the bitmap font's values
	GroupFont->SetValues(32, 32, 16, 14, ' ', FontImageID);

	CursorX = Game::GetInstance()->GetScreenWidth()/2;
	CursorY = 150;

	// Attempt to load the sound
	if(LoadSounds("SoundData.xml") == false)
	{
		musicVolume = (int)(XA->MusicGetMasterVolume()*100.0f);
		SFXVolume = (int)(XA->SFXGetMasterVolume()*100.0f);
	}
	else
	{
		XA->MusicSetMasterVolume((float)(musicVolume/100.0f));
		XA->SFXSetMasterVolume((float)(SFXVolume/100.0f));
	}
}

void MainMenuState::Exit(void)
{
	// Unload resource
	if(FontImageID != -1)
	{
		TM->UnloadTexture(FontImageID);
		FontImageID = -1;
	}

	// Set data members to 'clean' default values
	D3D = nullptr;
	DI  = nullptr;
	TM  = nullptr;
	XA  = nullptr;

	GroupFont = nullptr;

	CursorX = -1;
	CursorY = -1;
}

bool MainMenuState::Input(void)
{
	// Key Down
	if(DI->KeyPressed(DIK_DOWN) || DI->JoystickGetLStickDirPressed(DIR_DOWN))
		if(CursorY != 550)
			CursorY += 100;
		else
			CursorY = 150;

	// Key Up
	if(DI->KeyPressed(DIK_UP) || DI->JoystickGetLStickDirPressed(DIR_UP))
		if(CursorY != 150)
			CursorY -= 100;
		else
			CursorY = 550;

	// Go to quit when the user presses Escape
	if(DI->KeyPressed(DIK_ESCAPE) || DI->JoystickButtonPressed(6))
		CursorY = 550;

	// Enter the gameplay
	if((DI->KeyPressed(DIK_RETURN) || DI->JoystickButtonPressed(0)) && CursorY == 150)
		Game::GetInstance()->ChangeState(GamePlayState::GetInstance());
	else if((DI->KeyPressed(DIK_RETURN) || DI->JoystickButtonPressed(0)) && CursorY == 250)
		Game::GetInstance()->ChangeState(OptionsState::GetInstance());
	else if((DI->KeyPressed(DIK_RETURN) || DI->JoystickButtonPressed(0)) && CursorY == 350)
		Game::GetInstance()->ChangeState(TutorialState::GetInstance());
	else if((DI->KeyPressed(DIK_RETURN) || DI->JoystickButtonPressed(0)) && CursorY == 450)
		Game::GetInstance()->ChangeState(CreditsState::GetInstance());
	else if((DI->KeyPressed(DIK_RETURN) || DI->JoystickButtonPressed(0)) && CursorY == 550)
		return false;

	return true;
}

void MainMenuState::Update(float elapsed)
{

}

void MainMenuState::Render(void)
{
	// Selected and not selected colors
	DWORD isSelected = D3DCOLOR_ARGB(255,255,0,0);
	DWORD notSelected =  D3DCOLOR_ARGB(255,0,255,255);

	// Draw title
	GroupFont->Print("RoBHERT", 32, 32, 3.0f, D3DCOLOR_ARGB(255,0,255,255));
	//GroupFont->Print("1234567890", 32, 250, 1.0f, D3DCOLOR_ARGB(255,0,255,255));

	
	// Draw Menu items
	GroupFont->Print("Play", Game::GetInstance()->GetScreenWidth()/2-100, 150, CursorY==150 ? 2.0f : 1.0f, CursorY==150 ? isSelected : notSelected);
	GroupFont->Print("Options", Game::GetInstance()->GetScreenWidth()/2-100, 250, CursorY==250 ? 2.0f : 1.0f, CursorY==250 ? isSelected : notSelected);
	GroupFont->Print("How To Play", Game::GetInstance()->GetScreenWidth()/2-100, 350, CursorY==350 ? 2.0f : 1.0f, CursorY==350 ? isSelected : notSelected);
	GroupFont->Print("Credits", Game::GetInstance()->GetScreenWidth()/2-100, 450, CursorY==450 ? 2.0f : 1.0f, CursorY==450 ? isSelected : notSelected);
	GroupFont->Print("Quit", Game::GetInstance()->GetScreenWidth()/2-100, 550, CursorY==550 ? 2.0f : 1.0f, CursorY==550 ? isSelected : notSelected);
}

MainMenuState* MainMenuState::GetInstance(void)
{
	// Lazy instantiation
	static MainMenuState MainInstance;

	return &MainInstance;
}

bool MainMenuState::LoadSounds(const char* szFileName)
{
	// Create a TinyXML Document
	TiXmlDocument doc;
	
	// Attempt to load the document from the file
	if(doc.LoadFile(szFileName) == false)
		return false;

	// Access the root node (sound_info) in the document
	TiXmlElement* pRoot = doc.RootElement();
	if(pRoot == nullptr)
		return false;

	// Clear out any existing sound data
	musicVolume = -1;
	SFXVolume = -1;

	// Iterate through the root's children nodes (sound_info)
	TiXmlElement* pSound = pRoot->FirstChildElement("sound_info");
	while(pSound != nullptr)
	{
		// Read the attributes within the XML tag
		if(pSound->Attribute("music",&musicVolume) == nullptr)
			musicVolume = (int)(CSGD_XAudio2::GetInstance()->MusicGetMasterVolume()*100.0f);
		if(pSound->Attribute("sfx",&SFXVolume) == nullptr)
			SFXVolume = (int)(CSGD_XAudio2::GetInstance()->SFXGetMasterVolume()*100.0f);

		// Move to the next node in the player_list
		pSound = pSound->NextSiblingElement("sound_info");
	}
	
	return true;
}